﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Toodledo.Services.Interfaces;

namespace Toodledo.Services.Helpers
{
    public class ToodledoConnector : IToodledoConnector
    {
        private const string BASE_URL = "http://api.toodledo.com";

        public XDocument GetToodledoResponse(NameValueCollection queryParams)
        {
            var client = new WebClient();
            client.BaseAddress = BASE_URL;
            client.QueryString = queryParams;
            var responseStream = client.OpenRead("/api.php");
            using (var sr = new XmlTextReader(responseStream))
            {
                XDocument responseDoc = XDocument.Load(sr);
                if (responseDoc.Root.Name == "error")
                    throw new ToodledoException(responseDoc.Root.Value);
                return responseDoc;
            }
        }

    }
}
